//
//  MeViewController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "MeViewController.h"
#import "MyOrderViewController.h"
#import "MyCollectViewController.h"
#import "MyCouponViewController.h"
#import "ActivityViewController.h"
#import "ReviewViewController.h"
#import "LoginViewController.h"
#import "LoggedViewController.h"
#import "RegisterViewController.h"
#import "LoggedCell.h"
#import "AppSetting.h"


#define kLEFT       1001
#define kRIGHT      1002
#define kPERSONAL   1003
#define kMENU       1004
#define kLOGGED     1005
#define kLOGIN      1006

@implementation MeViewController

- (id)init
{
    self = [super initWithNibClass:[MeViewController class]];
    if (self) {
        _myMenu = [NSMutableArray arrayWithCapacity:10];
        _personalInfo = [NSMutableArray arrayWithCapacity:10];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([AppSetting isLogin]) {
        LoggedViewController *loggedVC = [[LoggedViewController alloc] init];
        [self.navigationController pushViewController:loggedVC animated:NO];
    }
}

- (void)setupUI
{
    [self initData];
    [self createLeftButton];
    [self createNavigation:@"个人会员中心"];
    [self createRightButtonWithTitle:@"" background:NAV_CLOSS_BACKGROUND];
}

- (void)initData
{
    self.menuListTV.scrollEnabled = NO;
    [_myMenu removeAllObjects];
    self.menuListTV.height = 340;
    NSMutableDictionary *review = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *albums = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myOrder = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myCoupon = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *activity = [NSMutableDictionary dictionaryWithCapacity:10];
    NSMutableDictionary *myCollect = [NSMutableDictionary dictionaryWithCapacity:10];
    
    [review setObject:@"点评" forKey:@"title"];
    [albums setObject:@"相册" forKey:@"title"];
    [activity setObject:@"活动" forKey:@"title"];
    [myOrder setObject:@"我的订单" forKey:@"title"];
    [myCollect setObject:@"我的收藏" forKey:@"title"];
    [myCoupon setObject:@"我的优惠券" forKey:@"title"];
    
    [review setObject:@"review_icon.png" forKey:@"icon"];
    [albums setObject:@"albums_icon.png" forKey:@"icon"];
    [myOrder setObject:@"order_icon.png" forKey:@"icon"];
    [myCoupon setObject:@"coupon_icon.png" forKey:@"icon"];
    [myCollect setObject:@"collect_icon.png" forKey:@"icon"];
    [activity setObject:@"activity_icon.png" forKey:@"icon"];

    [_myMenu addObject:myOrder];
    [_myMenu addObject:myCollect];
    [_myMenu addObject:myCoupon];
    [_myMenu addObject:activity];
    [_myMenu addObject:review];
    [_myMenu addObject:albums];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return 100;
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *CellIdentifier = [self setCellIdentifier:indexPath.row];
    
    LoggedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"LoggedCell" owner:self options:nil];
        cell = [array objectAtIndex:indexPath.row != 0 ? 1 : 2];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setViewBorder:indexPath.row];
    if (indexPath.row == 0) return cell;
    
    NSInteger row = indexPath.row-1;
    if ([self.myMenu count] <= row) return cell;
    
    NSDictionary* dic = [self.myMenu objectAtIndex:row];
    [cell setContect:dic];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self pushViewController:loginVC];
}

- (NSString*)setCellIdentifier:(int)tag
{
    if (tag != 0) return @"LoggedCell_1";
    return @"LoggedCell_2";
}

@end
